package com.shoprh.demo.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shoprh.demo.domain.Receiver;
import com.shoprh.demo.mapper.ReceiverMapper;
import com.shoprh.demo.service.IReceiverService;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 收货地址Service业务层处理
 *
 */
@Service
public class ReceiverServiceImpl extends ServiceImpl<ReceiverMapper, Receiver> implements IReceiverService {

    @Autowired
    private ReceiverMapper receiverMapper;

    /**
     * 查询收货地址
     *
     * @param id 收货地址ID
     * @return 收货地址
     */
    @Override
    public Receiver selectReceiverById(Long id) {
        return receiverMapper.selectById(id);
    }

    /**
     * 查询收货地址列表
     *
     * @param receiver 收货地址
     * @return 收货地址
     */
    @Override
    public List<Receiver> selectReceiverList(Receiver receiver) {
        return receiverMapper.selectList(Wrappers.emptyWrapper());
    }

    /**
     * 新增收货地址
     *
     * @param receiver 收货地址
     * @return 结果
     */
    @Override
    public int insertReceiver(Receiver receiver) {
        if (BooleanUtils.isTrue(receiver.getIsDefault())) {
            receiverMapper.clearDefault();
        }
        return receiverMapper.insert(receiver);
    }

    /**
     * 修改收货地址
     *
     * @param receiver 收货地址
     * @return 结果
     */
    @Override
    public int updateReceiver(Receiver receiver) {
        if (BooleanUtils.isTrue(receiver.getIsDefault())) {
            receiverMapper.clearDefault();
        }
        return receiverMapper.updateById(receiver);
    }

    /**
     * 删除收货地址对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteReceiverByIds(String ids) {
        return receiverMapper.deleteBatchIds(CollUtil.toList(ids));
    }

    /**
     * 删除收货地址信息
     *
     * @param id 收货地址ID
     * @return 结果
     */
    @Override
    public int deleteReceiverById(Long id) {
        return receiverMapper.deleteById(id);
    }
}
